<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改题目</title>
    <link rel="stylesheet" href="css/bootstrap1.min.css">
    <link rel="stylesheet" href="css/add.css">
</head>

<body>

<div class="container">
    <div class="form-inline">
        <h2 style="text-align: left; margin-left: 10px;"><svg xmlns="http://www.w3.org/2000/svg" width="40"
                                                              fill="#17a2b8" class="bi bi-book-half" viewBox="0 0 16 16">
            <path
                    d="M8.5 2.687c.654-.689 1.782-.886 3.112-.752 1.234.124 2.503.523 3.388.893v9.923c-.918-.35-2.107-.692-3.287-.81-1.094-.111-2.278-.039-3.213.492V2.687zM8 1.783C7.015.936 5.587.81 4.287.94c-1.514.153-3.042.672-3.994 1.105A.5.5 0 0 0 0 2.5v11a.5.5 0 0 0 .707.455c.882-.4 2.303-.881 3.68-1.02 1.409-.142 2.59.087 3.223.877a.5.5 0 0 0 .78 0c.633-.79 1.814-1.019 3.222-.877 1.378.139 2.8.62 3.681 1.02A.5.5 0 0 0 16 13.5v-11a.5.5 0 0 0-.293-.455c-.952-.433-2.48-.952-3.994-1.105C10.413.809 8.985.936 8 1.783z" />
        </svg>
            <span>修改题目</span>
        </h2>
    </div>

    <form id="updateProblem">
        <input type="hidden" class="form-control" id="problemId" name="id">
        <div class="form-group">
            <label for="title">题目名称:</label>
            <input type="text" class="form-control" id="title" name="title">
        </div>
        <div class="form-group">
            <label for="desc">题目描述</label>
            <input type="text" class="form-control" id="desc" name="description" />
        </div>
        <div class="form-group">
            <label for="level">题目难度</label>
            <input type="text" class="form-control" id="level" name="level" />
        </div>
        <div class="form-group" style="text-align: right">
            <button type="button" class="btn btn-info btn-lg" onclick="update()">确定</button>
            <button type="button" class="btn btn-secondary btn-lg" onclick="javascript:history.back()">返回</button>
        </div>
    </form>
</div>
<!--<script type="text/javascript" src="js/jquery.min.js"></script>-->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<script src="js/app.js"></script>
<script>
    $.ajax({
        type: "get",
        url: "/problem/getProblemStart" + location.search,
        success: function (result) {

            if(result.code == -1){
                window.location.href='login.html';
            }

            if (result != null && result.code == 200) {
                var problem = result.data;
                if (problem != null && problem.id != null) {
                    //页面输入框的填充
                    $("#problemId").val(problem.id);
                    $("#title").val(problem.title);
                    $("#desc").val(problem.description);
                    $("#level").val(problem.level);
                } else if(problem.id == null){
                    alert("题目不存在")
                    location.href='index.html';
                }
            } else {
                alert(result.errMsg);
            }
        },
        error: function (error) {
            console.log(error);
            //用户未登录
            if (error != null && error.status == 401) {
                location.href = "login.html";
            }

        }
    });

    function update() {
        $.ajax({
            type: "post",
            url: "/problem/updateProblem" + location.search,
            data: $("#updateProblem").serialize(),
            success: function (result) {
                if (result != null && result.code == 200 && result.data == true) {
                    location.href = "index.html";
                } else {
                    alert(result.errMsg);
                    location.href = "index.html";
                }
            },
            error: function (error) {
                console.log(error);
                //用户未登录
                if (error != null && error.status == 401) {
                    location.href = "login.html";
                }

            }
        });
    }


</script>
</body>

</html>